<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Session;
class AdminController extends Controller
{

    public function admin_login()
    {
        if (Session::has('username')) {
            return redirect('admin_show');
        } else {
            return view('Admin/login');
        }
    }

    public function admin_login(Request $r)
    {
        if ($r->isMethod('get')) {
            if (Session::has('user')) {
                return redirect('admin_show');
            } else {
                return view('admin.login');
            }
        } else {
            $info['msg'] = $this->validate($r, [
                'captcha' => 'required|captcha'
            ], [
                'captcha.required' => '不能为空',
                'captcha.captcha' => '不一致'
            ]);
            $data = $r->except('_token', 'nlogin');
            $res = DB::table('admin')->where('user', $data['user'])->where('pwd', $data['pwd'])->first();

            if (!!$res) {
                $nlogin = $r->post('nlogin');
                if ($nlogin == 1) {
                    Session::put('user', $res, time() + 7 * 24 * 3600);
                }
                if ($res->types == 1) {
                    return redirect('admin_show');
                } else {
                    return redirect('admin_select');
                }
            } else {
                return redirect('admin_login');
            }
        }
    }

    public function admin_show()
    {
        $re = DB::table('admins')->paginate(4);
        return view('Admin/show', ['re' => $re]);
    }

    public function admin_select()
    {
        $re = DB::table('admins')->paginate(4);
        return view('Admin/select', ['re' => $re]);
    }

    //删除
    public function admin_del($id)
    {
        $re = DB::table('admins')->where('id', $id)->delete();
        if ($re) {
            return redirect('admin_show');
        } else {
            return redirect()->back()->withInput()->withErrors('删除失败');
        }
    }

    //修改
    public function admin_upa(Request $r)
    {
        $data = array(
            'uname' => $r['uname'],
            'lei' => $r['lei'],
            'time' => $r['time']
        );
        $id = $_GET['id'];
        $re = DB::table('admins')->where('id', $id)->update($data);
        if ($re) {
            return response()->json([
                'status' => '200',
                'msg' => '修改成功'
            ]);
        } else {
            return response()->json([
                'status' => '201',
                'msg' => '修改失败'
            ]);
        }
    }

    //批量删除
    public function admin_dell(Request $r)
    {
        $id = $_GET['id'];
        /*$new_id=rtrim($id,',');*/
        $str = explode(",", $id);
        /*$info=DB::table('admins')->delete($str);*/
        foreach ($str as $v) {
            $info = DB::table('admins')->where('id', "=", "$v")->delete();
        }
        if ($info == 0) {
            return 1;
        } else {
            return 2;
        }
    }
}
    >